/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Business;

import Entities.Config;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author manhnv_b00954
 */
public class ConfigManage {

    public ConfigManage() {
    }
    public boolean SaveConfiguration(Config objConfig){
       
        boolean fag = false;
        try {
            PrintWriter write;
            write = new PrintWriter("configuration.properties");
            write.println("server = " + objConfig.getServerName() + "\nport = " + objConfig.getPortNumber() + "\ndatabase = " + objConfig.getDatabase() + "\nusername = " + objConfig.getUserName() + "\npassword = " + objConfig.getPassword());
            write.close();
            fag = true;
        } catch (FileNotFoundException ex) {
            fag = false;
            Logger.getLogger(ConfigManage.class.getName()).log(Level.SEVERE, null, ex);
        }
         return fag;
    }
    public Config getConfigProperties() {
        Config objConfig = null;
        try {
            Properties p = new Properties();
            FileInputStream fis;
            fis = new FileInputStream("configuration.properties");
            //This file "configuration.properties" contain the configuration of the system and
            // is located in the root directory of the project.
            p.load(fis);
            objConfig = new Config();
            objConfig.setServerName(p.getProperty("server"));
            objConfig.setPortNumber(Integer.parseInt(p.getProperty("port")));
            objConfig.setDatabase(p.getProperty("database"));
            objConfig.setUserName(p.getProperty("username"));
            objConfig.setPassword(p.getProperty("password"));
            
            //
        } catch (IOException ex) {
            System.out.println("Not found Files");
            Logger.getLogger(ConfigManage.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex){
            System.out.println("Not found server");
        }
        return objConfig;
    }

}
